package fzdy.core.common;

/**
 * Created by Administrator on 2017/3/27 0027.
 */

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReflectUtil {
    private static final String TAG = "ReflectUtils";

    public ReflectUtil() {
    }

    public static <T> T createData(Class<T> dataClass) {
        try {
            return dataClass.newInstance();
        } catch (InstantiationException var2) {
            var2.printStackTrace();
        } catch (IllegalAccessException var3) {
            dealWithException(var3);
        }

        return null;
    }

    public static Method getGetterMethodByField(String fieldName, Class c) {
        Method m = null;

        try {
            m = c.getMethod(StringUtil.getGetterMethodName(fieldName), new Class[0]);
        } catch (NoSuchMethodException var6) {
            try {
                m = c.getMethod(StringUtil.getGetterMethodNameBoolean(fieldName), new Class[0]);
            } catch (NoSuchMethodException var5) {
                ;
            }
        }

        return m;
    }

    public static Method getSetterMethodByField(String fieldName, Class c, Class arg) {
        Method m = null;
        String methodName = StringUtil.getSetterMethodName(fieldName);

        try {
            m = c.getMethod(methodName, new Class[]{arg});
            return m;
        } catch (NoSuchMethodException var13) {
            Class[] interfaces = arg.getInterfaces();
            Class[] var6 = interfaces;
            int var7 = interfaces.length;
            int var8 = 0;

            while(var8 < var7) {
                Class aClass = var6[var8];

                try {
                    m = c.getMethod(methodName, new Class[]{aClass});
                    return m;
                } catch (NoSuchMethodException var12) {
                    ++var8;
                }
            }

            Class superclass = arg;

            while(superclass != null && !Object.class.equals(superclass)) {
                try {
                    m = c.getMethod(methodName, new Class[]{superclass});
                    return m;
                } catch (NoSuchMethodException var11) {
                    superclass = superclass.getSuperclass();
                }
            }

            return m;
        }
    }

    public static Object invokeMethod(Method method, Object obj, Object... arg) {
        Object result = null;

        try {
            result = method.invoke(obj, arg);
        } catch (IllegalAccessException var5) {
            ;
        } catch (InvocationTargetException var6) {
            ;
        }

        return result;
    }

    public static List<Field> getAllFields(Class c) {
        ArrayList list = new ArrayList();

        do {
            Field[] fields = c.getDeclaredFields();
            Collections.addAll(list, fields);
        } while((c = c.getSuperclass()) != Object.class);

        return list;
    }

    private static void dealWithException(Exception e) {
    }
}
